package brainbots.gui;


import java.awt.*;
import java.awt.geom.AffineTransform;

import brainbots.Food;
import brainbots.ItemEnumerator;
import brainbots.World;
import brainbots.bot.BrainBot;
import brainbots.bot.Motor;

class BotsView{

    private World world;
    
    private BrainBot selectedBot=null;
    private boolean isAa = false;
    
    public final void setWorld(World w)
    {
        world = w;
    }
    
    public void setSelectedBot(BrainBot b)
    {
        selectedBot = b;
    }
    
    public void draw(final Graphics2D g)
    {/**Draw the world*/
    	if (world != null){
	        if (isAa){
	            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	                    RenderingHints.VALUE_ANTIALIAS_ON);
	        }
	        synchronized(world){
		        for (BrainBot bot: world.bots){
		            drawBot(g, bot);
		        }
		        for (int idx = 0; idx<world.food.size(); ++idx){
		            drawFood(g, world.food.get(idx));
		        }
	        }
    	}        
    }
    
    private void drawSelection(Graphics2D g, BrainBot b)
    {
        g.setColor(Color.green);
        g.drawOval((int)(b.getX()-20),(int)(b.getY()-20),40,40);
        
    }
    private void drawFood(Graphics2D g, Food f)
    {
        g.setColor(Color.green);
        g.drawOval((int)f.getX()-2, (int)f.getY()-2,4,4);
    }
    private void drawBot(Graphics2D g, BrainBot b)
    {
        AffineTransform oldTfm = g.getTransform();
        g.translate(b.getX(), b.getY());
        if (b == selectedBot){
            drawBotSelection(g);
        }
        g.transform(new AffineTransform(b.phi.cos, b.phi.sin, -b.phi.sin, b.phi.cos, 0,0)); //rotate
        
        
        drawBotContour(g, b);
        if (b == selectedBot){
        	drawBotSensors(b, g);
        }
        g.setTransform(oldTfm);
    }
    /**Graphical representation of bot sensor values*/
    private void drawBotSensors(BrainBot b, Graphics2D g) {
    	int numSens = b.getSensorCount();
    	g.setColor(Color.red);
    	for(int i = 0; i<numSens; ++i){
    		double v = b.getSensor(i);
    		int w = Math.min(20, (int)(Math.log(v+1.0)*10))+1;
    		
    		g.fillRect(-w, i*(-5)-15, w*2, 4);
    	}
	}

	private void drawBotSelection(Graphics2D g)
    {
        g.setColor(Color.green);
        g.drawOval(-20,-20,40,40);
    }
    private void drawBotContour(Graphics2D g, BrainBot b)
    {
    	int iconSize = 10; 
    	
        g.setColor(b.colors.get(2));
        g.fillArc(-iconSize, -iconSize, iconSize*2, iconSize*2, 0, 90);

        g.setColor(b.colors.get(3));
        g.fillArc(-iconSize, -iconSize, iconSize*2, iconSize*2, 90, 90);
        

        g.setColor(b.colors.get(4));
        g.fillArc(-iconSize, -iconSize, iconSize*2, iconSize*2, 180, 90);
        
        g.setColor(b.colors.get(5));
        g.fillArc(-iconSize, -iconSize, iconSize*2, iconSize*2, 270, 90);
        
        g.setColor(b.colors.get(0));
        g.drawOval(-10,-10,20,20);
        
        g.setColor(b.colors.get(1));
        g.fillRect(-2, 0, 4, 7);
        
        for (int i =0; i < b.getMotorCount(); ++i){
            Motor m = b.getMotor(i);
            drawMotor(g, m);
        }
    }
    private void drawMotor(Graphics2D g, Motor m)
    {
        if (m.force < 0.5){
            g.setColor(Color.black);
        }else{
            g.setColor(Color.red);
        }
        g.drawLine((int)(m.x*10), (int)(m.y*10), (int)((m.x+m.vx)*10), (int)((m.y+m.vy)*10));
    }

	public void selectBotAt(int x, int y) {
		selectedBot = world.pickBot(x, y);
	}

}
